//
//  StatusCell.swift
//  MicroBlog
//
//  Created by  NorthCity on 15/11/9.
//  Copyright © 2015年  NorthCity. All rights reserved.
//

import UIKit


/// 微博 cell 间距
let StatusCellMargin:CGFloat = 12
/// 微博 cell 头像大小
let StatusIconWidth:CGFloat =  35

/// 微博 cell
class StatusCell: UITableViewCell {
    ///视图模型
    var viewModel:StatusViewModel?{
        didSet{
          topView.viewModel = viewModel
          contentLabel.text = viewModel?.status.text
          pictureView.viewModel = viewModel
          pictureView.snp_updateConstraints { (make) -> Void in
             make.width.equalTo(pictureView.bounds.width)
             make.height.equalTo(pictureView.bounds.height)
            }
        }
    }
    // MARK: - 懒加载控件
    /// 顶部信息视图
     lazy var topView: StatusCellTopView = StatusCellTopView()
    /// 微博文字
     lazy var contentLabel: UILabel = UILabel(title: "微博文字", fontSize: 15, color: UIColor.darkGrayColor(),screenInsert: StatusCellMargin)
    /// 图片视图
     lazy var pictureView:StatusCellPictureView = StatusCellPictureView()
    /// 底部操作视图
     lazy var bottomView: StatusCellBottomView = StatusCellBottomView()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier:reuseIdentifier)
        setUpUI()
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK : - 计算行高
    func rowHeight(vm:StatusViewModel)->CGFloat{
        viewModel = vm
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
    

}
extension StatusCell{
        /// 设置界面
         func setUpUI() {
            
            // 0. 背景颜色
            backgroundColor = UIColor.whiteColor()
            // 1. 添加控件
            contentView.addSubview(topView)
            contentView.addSubview(contentLabel)
            contentView.addSubview(pictureView)
            contentView.addSubview(bottomView)
            
            // 2. 自动布局
            topView.snp_makeConstraints { (make) -> Void in
                make.top.equalTo(contentView.snp_top)
                make.left.equalTo(contentView.snp_left)
                make.right.equalTo(contentView.snp_right)
                make.height.equalTo(2*StatusCellMargin + StatusIconWidth)
            }
            contentLabel.snp_makeConstraints {(make) -> Void in
                make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
                make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
            }
                        
            bottomView.snp_makeConstraints { (make) -> Void in
                make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
                make.left.equalTo(contentView.snp_left)
                make.right.equalTo(contentView.snp_right)
                make.height.equalTo(44)
            }
                        
        }
    }
